<template>
<scroll-view scroll-y class="address">
    <!-- <view class="address"> -->
    <u-swipe-action>
    <u-swipe-action-item :options="options" @click="swiperItemClick" v-for="item in message" :key="item.id" :name="item.id">
    <view class="information">
      <view class="left">
        <view class="left-top">
          <span class="name">{{item.receiver}}</span>
          <span class="phone">{{item.contact}}</span>
          <span class="default" v-if="item.isDefault > 0">默认</span>
        </view>
        <view class="left-bottom">
          <p>{{item.fullLocation}}{{item.address}}</p>
        </view>
      </view>
      <view class="right">
        <navigator
          :url="`/pages/address/popupbox?id=${item.id}&isDefault=${item.isDefault}&isEdit=${isEdit}&title=修改地址`"
        >
        <u-icon name="edit-pen-fill" color="#909399" size="25"></u-icon>
        </navigator>
      </view>

    <!-- </view> -->
    </view>
    </u-swipe-action-item>
    </u-swipe-action>
   
    <!-- <u-button class="uButton" type="primary" color="#27ba9b" shape="circle" text="新建地址"></u-button> -->
    <button class="newbut" @click="newProject">新建地址</button>
</scroll-view>
</template>

<script>
import { setHeader } from "../../utils/request";
export default {
  data() {
    return {
      code: "",
      message:[],
      isEdit:true, // 真是修改,假是新建
       options: [
        {
          text: "删除",
          style: {
            backgroundColor: "#f56c6c",
          },
        },
      ],
    };
  },
 async onLoad() {
    await this.loginWxmin(); //获取 token
    await this.getAConsigneeddress(); //获取收货地址
  },
  methods: {
    // 获取收货地址列表
    async getAConsigneeddress() {
      const res = await uni.$u.http.get("/member/address");
      // console.log(res.result);
      this.message = res.result
      
    },
    // 发请求或取token
    async loginWxmin() {
      try {
        // 登录请求
        const {
          result: { token },
          result,
        } = await uni.$u.http.post("/login/wxMin/simple", {
          phoneNumber: "16868686868",
        });
        // console.log(result,999);
        setHeader({ Authorization: token });
        wx.setStorage(userInfo, result);
      } catch (error) {}
    },
    // 删除请求
     async deleteMemberAddress(id){
     const res = await uni.$u.http.delete(`/member/address/${id}`)
     this.getAConsigneeddress() // 删除之后重新渲染
    // console.log(res);
     if(res.msg === "操作成功"){
        uni.showToast({
        title:res.msg,
        icon:"none"
      })
     }
    },
   // 点击删除
   swiperItemClick({name}){
    this.deleteMemberAddress(name) 
   },
   // 新建
   newProject(){
     uni.navigateTo({
          url:"/pages/address/popupbox"
      })
   }
  },
};
</script>

<style lang="scss">
.address {
  height: 100vh;
  padding: 20rpx;
  background-color: #f4f4f4;
  .information {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%rpx;
    height: 110rpx;
    padding: 30rpx;
    margin-bottom: 5rpx;
    background-color: #fff;
  }
  .left {
    .left-top {
      margin-bottom: 15rpx;
      .phone {
        margin: 0 10rpx;
        color: #767676;
      }
      .default {
        font-size: 20rpx;
        margin-left: 5rpx;
        padding: 3rpx 12rpx;
        color: #adadad;
        border: 1px #666 solid;
        border-radius: 8rpx;
      }
    }
  }
  .newbut{
    position:fixed; 
    z-index: 10; // 提高层级
    // visibility: hidden;
    bottom:45rpx;
    width: 710rpx;
    height: 80rpx;
    background-color:#27ba9b;
    border-radius: 40rpx;
    font-size: 30rpx;
    color: #fff;
    margin: 10rpx 5rpx;
  }
}
</style>
